package com.lry.pda.bottom.c.backend.executor;

import com.lry.pda.bottom.c.backend.code.CodeKey;
import com.lry.pda.bottom.c.backend.code.CodeNode;
import com.lry.pda.bottom.c.frontend.Grammar;

import java.util.ArrayList;
import java.util.List;

public class ArgExecutor extends BaseExecutor{

    @Override
    public void execute(CodeNode root) {
        int production = (Integer)root.getAttr(CodeKey.PRODUCTION);
        List<Object> argList = new ArrayList<>();
        CodeNode child ;

        if(production== Grammar.NO_COMMA_EXPR_TO_ARGS){
            child = executeChild(root, 0);
            Object val = child.getAttr(CodeKey.VALUE);
            argList.add(val);
        }else if(production==Grammar.NO_COMMA_EXPR_COMMA_ARGS_TO_ARGS){
            child = executeChild(root, 0);
            Object val = child.getAttr(CodeKey.VALUE);
            argList.add(val);

            child = executeChild(root, 1);
            List<Object> list = (ArrayList<Object>)child.getAttr(CodeKey.VALUE);
            argList.addAll(list);
        }
        root.setAttr(CodeKey.VALUE, argList);
    }
}
